/**
 * 
 */
package com.verisette.ncos.web.card.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.corp.agi.web.action.BaseAction;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

import com.verisette.ncos.common.ProjectConstant;

/**
 * @author Pongsatorn Paolanwat (Mix)
 * 
 */
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ImageDispatchAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(ImageDispatchAction.class);
	
	private InputStream inputStream;
//	private HttpServletRequest httpRequest;
//	private HttpServletResponse httpResponse;
	private String contentType;
	private String contentDisposition;
	
	private String name;
	private String isTemp = "N";
	
	public String execute(){
		
    	String fileName = null;
    	try {
    		
    		// create file
    		String path = ProjectConstant.APP_TEMP_IMG_FOLDER;
			File theFile = new File(path+"\\"+name);
			if(logger.isDebugEnabled()) logger.debug("Read image file:"+theFile.getName());
			if (theFile.exists() && theFile.isFile()) {
				if(name.toLowerCase().endsWith("jpg")) {
					contentType="image/jpeg";
				} else {
					contentType="image/"+name.split("\\.")[1];
				}
			} else { // image not found
				theFile = new File(path+"\\picture.jpg");
				contentType="image/jpeg";
			}
			
			inputStream = new FileInputStream(theFile);
			// set extension
			fileName = theFile.getName();
			
			// set File-name to UTF-8
			fileName = URLEncoder.encode(fileName,"UTF-8");
			
			contentDisposition="filename="+fileName+";"; 
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			try {
				String path = ProjectConstant.APP_TEMP_IMG_FOLDER;
				File theFile = new File(path+"\\picture.jpg");
				contentType="image/jpeg";
			
				inputStream = new FileInputStream(theFile);
			
				// set extension
				fileName = theFile.getName();
				
				// set File-name to UTF-8
				fileName = URLEncoder.encode(fileName, "UTF-8");
				
				
				contentDisposition="filename="+fileName+";";
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				logger.error(e1.getMessage(), e1);
			} catch (FileNotFoundException e2) {
				// TODO Auto-generated catch block
				logger.error(e2.getMessage(), e2);
			}
		}
    	return SUCCESS;
    }

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public String getIsTemp() {
		return isTemp;
	}

	public void setIsTemp(String isTemp) {
		this.isTemp = isTemp;
	}
}
